package com.spontaneappnew;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Category{
	 
	 public static final int TYPE_NORMAL = 0;
	 public static final int TYPE_SELECT = 1;
	 public static final int TYPE_HEADING = 2;
	 
	 private String id = null;
	 private String name = null;
	 private byte[] icon;
	 private Bitmap marker;
	 private boolean selected = false;
	 private boolean isSelectRow = false;
	 private boolean isHeading = false;
	 
	 public Category(String id, String name, byte[] icon, byte[] marker)  {
	  this.id = id;
	  this.name = name;
	  this.icon = icon;
	  this.marker = BitmapFactory.decodeByteArray(marker,0,marker.length);
	 }
	 
	 /****
	  * this Constructor is for selected rows and will hold the id of the category and name. This is for the adapter.
	  ****/
	 public Category(String id, String name, boolean isSelectRow){
		 this.id = id;
		 this.name = name;
		 this.isSelectRow = isSelectRow; 
	 }
	 
	 public Bitmap getMarker() {
		return marker;
	}

	public void setMarker(byte[] marker) {
		this.marker = BitmapFactory.decodeByteArray(marker,0,marker.length);
	}

	/****
	  * this Constructor is for heading and will hold the name of the heading in the name field. This is for the adapter.
	  ****/
	 public Category(String label){
		this.isHeading=true;
		this.name=label;
	 }
	 
	public String getID() {
	     return id;
	 }
	 
	 public void setID(String id) {
	     this.id = id;
	 }
	 
	 public String getName() {
	     return name;
	 }
	 
	 public void setName(String name) {
	     this.name = name;
	 }
	  
	 public byte[] getImage(){
		 return icon;
	 }
	 
	 public void setImage(byte[] icon){
		 this.icon = icon;
	 }
	 
	 public boolean isSelected() {
		 return selected;
	 }
	 
	 /*
	  * Gets a boolean and set the category to be selected, handles the Header of the selected list
	  */
	 public void setSelected(boolean selected) { 
		  if (selected){
			  if (Categories.getInstance().getOffset()==0){
				  Categories.getInstance().getCategories().add(0,new Category("Selected"));
			  }
			  Categories.getInstance().incOffset();
		  }
		  else {	  
			  if (this.selected==true){
				  Categories.getInstance().decOffset();
				  if (Categories.getInstance().getOffset()==0)
					  Categories.getInstance().getCategories().remove(0);
			  }
			  
		  }
		  this.selected = selected;
	 }
	 
	 /*
	  * Returns the type of the category
	  */
	 public int getType(){
		 if (this.isSelectRow==true) 
				return TYPE_SELECT; 
		else if (this.isHeading)
				return TYPE_HEADING;
		else return TYPE_NORMAL;
	 }
	 
	 /*
	  * Set the category to be unselected
	  */
	 public void unSelect(){
		 for (Category category : Categories.getInstance().getCategories()){
			 if (category.getID()==this.getID() && category.getType()==TYPE_NORMAL){
				 category.setSelected(false);
				 return;
			 }
		 }	 
	 }
}